<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use EloquentFilter\Filterable;

/**
 * App\Models\Article
 *
 * @property int $id
 * @property int $node_id
 * @property int $user_id
 * @property string $title
 * @property string|null $keywords
 * @property string|null $description
 * @property int $status
 * @property int $pinned
 * @property int $allow_comment
 * @property int $views_count
 * @property int $likes_count
 * @property int $comments_count
 * @property \Illuminate\Support\Carbon|null $pinned_at
 * @property \Illuminate\Support\Carbon|null $frozen_at
 * @property \Illuminate\Support\Carbon|null $published_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @property-read \App\Models\Content $content
 * @property-read mixed $has_frozen
 * @property-read mixed $has_pinned
 * @property-read \App\Models\Node $node
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Tag[] $tags
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article filter($input = array(), $filter = null)
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Article onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereAllowComment($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereCommentsCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereFrozenAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereKeywords($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereLikesCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereNodeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article wherePinned($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article wherePinnedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article wherePublishedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereViewsCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Article withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Article withoutTrashed()
 * @mixin \Eloquent
 */
class Article extends Model
{
    use SoftDeletes, Filterable;

    /**
     * @var array
     */
    protected $fillable = [
        'user_id', 'title', 'node_id',
        'pinned_at', 'frozen_at', 'published_at',
        'views_count', 'likes_count', 'comments_count',
        'status', 'allow_comment', 'pinned',
        'keywords', 'description',
    ];

    /**
     * @var array
     */
    protected $casts = [
        'id' => 'int',
        'user_id' => 'int',
        'views_count' => 'int',
        'likes_count' => 'int',
        'comments_count' => 'int',
        'allow_comment' => 'int',
        'status' => 'int',
        'pinned' => 'int',
    ];

    const STATUS_PUBLISHED = 1; // 发布
    const STATUS_UNPUBLISHED = 0; // 未发布
    const STATUS_DRAFT = 2; // 草稿

    protected $dates = [
        'pinned_at', 'frozen_at', 'published_at',
    ];

    /**
     * @var array
     */
    protected $with = ['user'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function node()
    {
        return $this->belongsTo(Node::class);
    }


    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }


    public function content()
    {
        return $this->morphOne(Content::class, 'contentable');
    }


    public function getHasPinnedAttribute()
    {
        return (bool)$this->pinned_at;
    }

    public function getHasFrozenAttribute()
    {
        return (bool)$this->frozen_at;
    }
}